﻿@using RooHui.Infrastructure.Entities.Entity;
@using RooHui.Infrastructure.Entities.Enum
@using RooHui.OA.Biz;
@using RooHui.Tool
@{
    ViewBag.Title = "ProjectAudit";
    Layout = "~/Views/Shared/_Base.cshtml";
    List<ProcessStepDoing> processStepList = ViewBag.processStepList;
    Project project = ViewBag.project;
    ProcessDoing processDoing = ViewBag.processDoing;
    List<ProcessDoingHistory> processDoingHistoryList = ViewBag.processDoingHistoryList;
    List<RepaymentPlan> repaymentPlanList = ViewBag.repaymentPlanList;
    Policy policy = project.PolicyList.FirstOrDefault(c => !c.IsAttach);
    int cycleAmount = policy.CycleAmount;
    if (policy.CycleAmount == 0)
    {
        cycleAmount = project.CycleAmount;
    }

    DateTime startDate = DateTime.Now;
    

    decimal borrowTotalInterest = RepaymentPlanBiz.Round(repaymentPlanList.Where(c => !c.Policy.IsAttach).ToList().Sum(c => c.RepaymentInterest),2);

    
    decimal investTotalInterest = RepaymentPlanBiz.Round((borrowTotalInterest / project.OriginalRate) * policy.Rate,2);
    decimal guaranteeTotalInterest = RepaymentPlanBiz.Round((borrowTotalInterest / project.OriginalRate) * project.GuaranteeRate, 2);
    if(policy.BalanceType==BalanceType.AverCapitalInterest&&policy.SettlementType == SettlementType.Proportion)
    {

        decimal monthRate = policy.Rate / 12 / 100;
        decimal investmonthtotal = (project.Financing * monthRate * (decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount)) / ((decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount) - 1);

        investTotalInterest = cycleAmount * investmonthtotal - project.Financing;
        
        monthRate = project.GuaranteeRate/ 12 / 100;
        decimal guaranteemonthtotal = (project.Financing * monthRate * (decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount)) / ((decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount) - 1);
        guaranteeTotalInterest = cycleAmount * guaranteemonthtotal - project.Financing;
    }

    decimal monthRate1 = (project.OriginalRate - project.GuaranteeRate - policy.Rate) / 12 / 100;
    decimal reward = repaymentPlanList.Where(c => c.Policy.IsAttach).ToList().Sum(c => c.RepaymentInterest);
    //decimal profits = cycleAmount *((project.Financing * monthRate1 * (decimal)Math.Pow((double)(1 + monthRate1), (double)cycleAmount)) / ((decimal)Math.Pow((double)(1 + monthRate1), (double)cycleAmount) - 1)) - project.Financing;
    decimal profits = borrowTotalInterest - guaranteeTotalInterest - investTotalInterest;
    decimal lastprofits = profits - reward;
}
<input type="hidden" name="projectID" id="projectID" value="@project.ID" />
<table border="0" width="99%" class="tablel" style="margin-top: 5px">
    <tr>
        <td align="right" width="80px">
            当前工作:
        </td>
        <td align="left" width="200px">
            @processDoing.ProcessName
        </td>
        <td align="right" width="80px">
            我的审核:
        </td>
        <td align="left" width="200px">
            <select id="selProcessStep" class="selectpicker" data-style="btn-info">
                @{
                    string signcontent = "";
                    if (processStepList != null && processStepList.Count > 0)
                    {
                        signcontent = "同意 " + processStepList[0].StepName;
                        foreach (var item in processStepList)
                        {
                    <option value="@item.ID">@item.StepName</option>
                        }
                    }
                }
                <option value="-1">审核不通过</option>
            </select>
        </td>
        <td align="right" width="80px">
            当前审核人:
        </td>
        <td align="left">
            @processDoing.User.RealName
        </td>
    </tr>
    <tr>
        <td align="right">
            审核意见:
        </td>
        <td align="left" colspan="7">
            <textarea rows="3" class="l-textarea" style="width: 99%" id="txtAuditContent">@signcontent</textarea>
        </td>
    </tr>
    <tr>
        <td align="right">
        </td>
        <td align="left" colspan="7">
            <button id="btnSave" class="btn btn-primary btn-small" style="margin-right: 5px">
                提交
            </button>
            <input  type="button" value="取消" id="btnCancel" class="btn  btn-small" />
        </td>
    </tr>
</table>
<div id="accordion" class="accordion-style1 panel-group">
    <div class="panel panel-default" style="margin: 5px">
        <div class="panel-heading">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                &nbsp;项目信息 </a>
        </div>
        <div class="panel-collapse collapse" id="collapseOne">
            <div class="panel-body">
                <table cellpadding="0" cellspacing="0" class="tablel" width="100%">
                    <tr>
                        <td align="right" width="9%">
                            产品名称:
                        </td>
                        <td align="left" width="16%">
                            <span style="color: #000079;">
                                @project.ProductName
                            </span>
                        </td>
                        <td align="right" width="9%">
                            项目名称:
                        </td>
                        <td align="left" width="16%">
                            <span style="color: #000079;">
                                @project.ProjectTitle
                            </span>
                        </td>
                        <td align="right" width="9%">
                            项目代码:
                        </td>
                        <td align="left" width="16%">
                            <span style="color: #000079;">
                                @project.ProjectCode
                            </span>
                        </td>
                        <td align="right" width="9%">
                            投资开放时间:
                        </td>
                        <td align="left" width="16%">
                            <span style="color: #000079;">
                                @project.WaitDay 天</span>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            融资金额:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @(Math.Round(project.Financing / 10000, 2, MidpointRounding.AwayFromZero))
                            </span>万元
                        </td>
                        <td align="right">
                            投资起点金额:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @project.MinInvestment.ToString("F2")</span>元
                        </td>
                        <td align="right">
                            递增单位金额:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @project.IncreaseMoney.ToString("F2")</span>元
                        </td>
                        <td align="right">
                            借款时间:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @(project.CycleAmount * project.PolicyList.FirstOrDefault(c => !c.IsAttach).Duration)@(project.PolicyList.FirstOrDefault(c => !c.IsAttach).DurationType.Mapping())</span>
                            （@project.CycleAmount 期 * @project.PolicyList.FirstOrDefault(c => !c.IsAttach).Duration
                            @project.PolicyList.FirstOrDefault(c => !c.IsAttach).DurationType.Mapping() /期）
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            结算方式:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @policy.BalanceType.Mapping()</span>
                        </td>
                        <td align="right">
                            结算周期:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @policy.Duration
                                @(policy.DurationType.Mapping())</span>
                        </td>
                        <td align="right">
                            创建人:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @project.User.RealName</span>
                        </td>
                        <td align="right">
                            创建时间:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @project.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")</span>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            借款年化利率:
                        </td>
                        <td align="left">
                            <span style="color: Red;">@(project.OriginalRate)%</span>
                        </td>
                        <td align="right">
                            借款总利息:
                        </td>
                        <td align="left">
                            <span style="color: Red;">@borrowTotalInterest.ToString("f2")</span>元
                        </td>
                        <td align="right" rowspan="3">
                            额外奖励:
                        </td>
                        <td align="left" colspan="3" rowspan="3">
                            @{
                                
                                if (project.PolicyList != null && project.PolicyList.Count > 0)
                                {

                                    var attachPolicys = project.PolicyList.Where(c => c.IsAttach).ToList();

                                    if (attachPolicys != null && attachPolicys.Count > 0)
                                    {

                                <ul>
                                    @{
                                         
                                        foreach (var item in attachPolicys)
                                        {
                                            int duration = item.Duration;
                                            DurationType durationType = item.DurationType;
                                            if (duration == 0)
                                            {
                                                duration = policy.Duration;
                                                durationType = policy.DurationType;
                                            }
                                            
                                        <li><strong style="color: Red;">额外追加 @item.Rate @(item.SettlementType == SettlementType.Proportion ? "% 年化收益率" : "元固定收益")
                                            (@(item.CycleAmount == 0 ? project.CycleAmount : item.CycleAmount)期*@duration@durationType.Mapping()/期)</strong></li>
                                            

                                        }
                                         
                                    }
                                </ul>
                                    }
                                    else
                                    {
                                <span style="color: Red;">无 </span>
                                    }

                                }
                            }
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            投资年化利率:
                        </td>
                        <td align="left">
                            <span style="color: Red;">
                                @(policy.Rate)% </span>
                        </td>
                        <td align="right">
                            投资总收益:
                        </td>
                        <td align="left">
                            <span style="color: Red;">
                                @investTotalInterest.ToString("f2")</span>元
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            担保金年化利率:
                        </td>
                        <td align="left">
                            <span style="color: Red;">
                                @(project.GuaranteeRate)%</span>
                        </td>
                        <td align="right">
                            担保金:
                        </td>
                        <td align="left">
                            <span style="color: Red;">
                                @guaranteeTotalInterest.ToString("f2")</span>元
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            平台收益率:
                        </td>
                        <td align="left">
                            <span style="color: #000079; font-weight: bold; font-size: 17px;">
                                @(project.OriginalRate - policy.Rate - project.GuaranteeRate)%</span>
                        </td>
                        <td align="right">
                            平台收益金额:
                        </td>
                        <td align="left">
                            <span style="color: #000079; font-weight: bold; font-size: 17px;">@profits.ToString("F2")</span>元
                        </td>
                        <td align="right">
                            奖励金额:
                        </td>
                        <td align="left">
                            <span style="color: #000079; font-weight: bold; font-size: 17px;">
                                @reward.ToString("F2")</span>
                        </td>
                        <td align="right">
                            平台最终收益:
                        </td>
                        <td align="left">
                            <span style="color: #000079; font-weight: bold; font-size: 17px;">
                                @lastprofits.ToString("F2")</span>元
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            担保公司:
                        </td>
                        <td align="left">
                            <span style="color: #000079;">
                                @project.GuaranteeCompany.CompanyName</span>
                        </td>
                        <td align="right">
                            担保意见:
                        </td>
                        <td align="left" colspan="5">
                            @project.CollateralOpinion
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            风险控制:
                        </td>
                        <td align="left" colspan="7">
                            @project.RiskCotrolSummary
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            项目描述:
                        </td>
                        <td align="left" colspan="7">
                            @project.ProjectDescription
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <div class="panel panel-default" style="margin: 5px">
        <div class="panel-heading">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion"
                href="#collapseTwo">&nbsp;借款企业 </a>
        </div>
        <div class="panel-collapse collapse" id="collapseTwo">
            <div class="panel-body">
                <table cellpadding="0" cellspacing="0" class="tablel" style="margin-right: 2px">
                    <tr>
                        <td align="right" width="9%">
                            企业名称:
                        </td>
                        <td align="left" width="30%">
                            <span style="color: #000079;">
                                @project.Enterprise.EnterpriseName
                            </span>
                        </td>
                        <td align="right" width="9%">
                            企业代码:
                        </td>
                        <td align="left" width="20%">
                            <span style="color: #000079;">
                                @project.Enterprise.EnterpriseCode
                            </span>
                        </td>
                        <td align="right" width="9%">
                            企业信用:
                        </td>
                        <td align="left" width="20%">
                            <span style="color: #000079;">
                                @project.Enterprise.EnterpriseCredit
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            企业信息:
                        </td>
                        <td align="left" colspan="7">
                            @project.Enterprise.EnterpriceInfo
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            资金用途:
                        </td>
                        <td align="left" colspan="7">
                            @project.Enterprise.CapitalOperation
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <div class="panel panel-default" style="margin: 5px">
        <div class="panel-heading">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion"
                href="#collapseThree">&nbsp;相关资料</a>
        </div>
        <div class="panel-collapse collapse" id="collapseThree">
            <div class="panel-body">
                <table cellpadding="0" cellspacing="0" class="tablel" width="100%">
                    @{
                        if (project.ResourceList != null && project.ResourceList.Count > 0)
                        {
                            var resourceListTemp = project.ResourceList.GroupBy(c => c.SubType).ToList();
                            foreach (var item in resourceListTemp)
                            {
                                string resourceType = item.Key.Mapping();
                                var resources = project.ResourceList.Where(c => c.SubType == item.Key).ToList();
                        <tr style="font-size: 14px; font-weight: bold; color: #000079;">
                            <td colspan="5">@resourceType
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @if (resources != null && resources.Count > 0)
                                {
                                    string imageExtend = ".jpg|.bmp|.gif|.png|.jpeg";
                                    foreach (var res in resources)
                                    {
                                        if (imageExtend.Contains(res.ExtendName.ToLower()))
                                        {
                                    <img width="173" height="98" src="@res.Path"/>
                                        }
                                        else
                                        {
                                    <a href="@res.Path">@res.Name</a>
                                        }
                                    }
                                }
                            </td>
                        </tr>
                            }
                        }
                    }
                </table>
            </div>
        </div>
    </div>
    <div class="panel panel-default" style="margin: 5px">
        <div class="panel-heading">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion"
                href="#collapseFour">&nbsp;还款计划</a>
        </div>
        <div class="panel-collapse collapse" id="collapseFour">
            <div class="panel-body">
                @{
                    if (repaymentPlanList != null && repaymentPlanList.Count > 0)
                    {
                        foreach (var item in repaymentPlanList)
                        {
                            string policyStr = "";
                            decimal rate = 0m;
                            if (item.Policy.IsAttach)
                            {
                                policyStr = "平台奖励";
                                rate = item.Policy.Rate;
                            }
                            else
                            {
                                rate = project.OriginalRate;
                            }
                   
                        
                    <div style="margin: 10px;">
                        <span style="color: Red; font-size: 16px;">
                            @policyStr @rate @(item.Policy.SettlementType == SettlementType.Proportion ? "% 年化利率" : "元固定还款")</span>
                        (@(item.Policy.CycleAmount == 0 ? project.CycleAmount : item.Policy.CycleAmount)期*@item.Policy.Duration@item.Policy.DurationType.Mapping()/期)<span
                            style="color: #000079; font-size: 16px;">
                            @item.Policy.BalanceType.Mapping()</span>的还款计划,计息日为:@DateTime.Now.ToString("yyyy-MM-dd")
                        <table cellpadding="0" cellspacing="0" class="tablel" width="100%">
                            <tr style="font-size: 14px; font-weight: bold; color: #000079;">
                                <td align="center" width="5%">
                                    期数
                                </td>
                                <td align="center" width="8%">
                                    还款日
                                </td>
                                <td align="center" width="8%">
                                    计息天数
                                </td>
                                <td align="center" width="12%">
                                    偿还本金(&yen)
                                </td>
                                <td align="center" width="12%">
                                    偿还利息(&yen)
                                </td>
                                <td align="center" width="13%">
                                    偿还本息和(&yen)
                                </td>
                                <td align="center" width="12%">
                                    剩余本金(&yen)
                                </td>
                                <td align="center" width="12%">
                                    剩余利息(&yen)
                                </td>
                                <td align="center" width="13%">
                                    剩余本息和(&yen)
                                </td>
                            </tr>
                            @if (item.RepaymentPlanItemList != null && item.RepaymentPlanItemList.Count > 0)
                            {
                                <tr style="font-size: 13px; font-weight: bold; color: #e86e00;">
                                    <td align="center">
                                    </td>
                                    <td align="center">
                                    </td>
                                    <td align="center">
                                    </td>
                                    <td align="center">
                                    </td>
                                    <td align="center">
                                    </td>
                                    <td align="center">
                                    </td>
                                    <td align="center">
                                        @(item.RepaymentPrincipal.ToString("f2"))
                                    </td>
                                    <td align="center">
                                        @(item.RepaymentInterest.ToString("f2"))
                                    </td>
                                    <td align="center">
                                        @((item.RepaymentPrincipal + item.RepaymentInterest).ToString("f2"))
                                    </td>
                                </tr>
                                foreach (var planItem in item.RepaymentPlanItemList)
                                {
                                <tr style="font-size: 12px; color: #000079;">
                                    <td align="center">
                                        @planItem.CycleNumber
                                    </td>
                                    <td align="center">
                                        @planItem.RepaymentDate.ToString("yyyy-MM-dd")
                                    </td>
                                    <td align="center">
                                        @planItem.SpanDay
                                    </td>
                                    <td align="center">
                                        @planItem.RepaymentPrincipal.ToString("f2")
                                    </td>
                                    <td align="center">
                                        @planItem.RepaymentInterest.ToString("f2")
                                    </td>
                                    <td align="center">
                                        @((planItem.RepaymentPrincipal + planItem.RepaymentInterest).ToString("f2"))
                                    </td>
                                    <td align="center">
                                        @planItem.ReMainPrincipal.ToString("f2")
                                    </td>
                                    <td align="center">
                                        @planItem.ReMainInterest.ToString("f2")
                                    </td>
                                    <td align="center">
                                        @((planItem.ReMainPrincipal + planItem.ReMainInterest).ToString("f2"))
                                    </td>
                                </tr>
                                }
                            }
                        </table>
                    </div>
                        }
                    }
                }
            </div>
        </div>
    </div>
    <div class="panel panel-default" style="margin: 5px">
        <div class="panel-heading">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion"
                href="#collapseFive">&nbsp;审核过程</a>
        </div>
        <div class="panel-collapse collapse" id="collapseFive">
            <div class="panel-body">
                <table cellpadding="0" cellspacing="0" class="tablel" width="100%">
                    <tr style="font-size: 14px; font-weight: bold; color: #000079;">
                        <td align="center" width="20%">
                            当前工作
                        </td>
                        <td align="center" width="10%">
                            办理状态
                        </td>
                        <td align="center">
                            办理意见
                        </td>
                        <td align="center" width="10%">
                            办理人
                        </td>
                        <td align="center" width="20%">
                            办理时间
                        </td>
                    </tr>
                    @{
                        if (processDoingHistoryList != null && processDoingHistoryList.Count > 0)
                        {
                            foreach (var item in processDoingHistoryList)
                            {
                                string doState = "审核通过";
                                if (item.DoDirection == -1)
                                {
                                    doState = "审核不通过";
                                }
                        <tr>
                            <td>@item.ProcessDoing.ProcessName
                            </td>
                            <td align="center">@doState
                            </td>
                            <td>@item.DoingContent
                            </td>
                            <td align="center">@item.User.RealName
                            </td>
                            <td align="center">@item.DoingTime.ToString("yyyy-MM-dd HH:mm:ss")
                            </td>
                        </tr>
                            }
                        }
                    }
                </table>
            </div>
        </div>
    </div>
</div>
@section tail{
    <script type="text/javascript">
        var selProcessStep;
        $(function () {

            selProcessStep = $("#selProcessStep").selectpicker('show');
            $('#selProcessStep').change(function () {
                if ($(this).children('option:selected').val() == -1) {
                    $("#txtAuditContent").text($(this).children('option:selected').text());
                }
                else {
                    $("#txtAuditContent").text('同意 ' + $(this).children('option:selected').text());
                }
            })

            $("#btnSave").click(function () {

                $("#btnSave").text("处理中...");
                $("#btnSave").attr("disabled", true);
                $.ajax({
                    url: "/ProjectAudit/ProjectAuditDo",
                    data: "projectID=" + $("#projectID").val() + "&processStepDoingID=" + selProcessStep.val()
                    + "&doContent=" + $("#txtAuditContent").val(),
                    type: "post",
                    success: function (result) {

                        //写提交ajax操作
                        if (result == "1") {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>&nbsp;&nbsp;审核成功</span>", '300px', function () {
                                location.href = decodeURIComponent("/ProjectAudit/Index");
                            });

                        }
                        else {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-warning-sign bigger-290'></i>&nbsp;&nbsp;审核失败</span>", '300px');

                        }
                        $("#btnSave").text("提交");
                        $("#btnSave").attr("disabled", false);

                    }
                });


            });
            $("#btnCancel").click(function () { location.href = decodeURIComponent("/ProjectAudit/Index"); });
        });
    </script>
}
